@charset "UTF-8";

// GTK functions

// escape string entirely
@function gtk($str) {
    @return unquote("#{$str}");
}

// alpha function
@function gtkalpha($c, $a) {
    @return unquote("alpha(#{$c},#{$a})");
}

// shade function
@function gtkshade($c, $a) {
    @return unquote("shade(#{$c},#{$a})");
}

// mix function
@function gtkmix($c1, $c2, $m) {
    @return unquote("mix(#{$c1},#{$c2},#{$m})");
}

//
// Theme color functions
//

// Default Color
@function internal_default($c: background) {
    @if $c == background {
        @return $DefaultBackgroundNormal;
    }
    @if $c == foreground {
        @return $DefaultForegroundNormal;
    }
    @if $c == border {
        @return $DefaultBorder;
    }
    @if $c == warning {
        @return $DefaultWarning;
    }
    @if $c == error {
        @return $DefaultError;
    }
    @if $c == success {
        @return $DefaultSuccess;
    }
    @if $c == link {
        @return $DefaultLink;
    }
    @if $c == link-visited {
        @return $DefaultLinkVisited;
    }
}

// Widget Color
@function internal_widget($c: background) {
    @if $c == background {
        @return $WidgetBackgroundNormal;
    }
    @if $c == foreground {
        @return $WidgetForegroundNormal;
    }
    @if $c == background-hover {
        @return $WidgetBackgroundHover;
    }
    @if $c == background-active {
        @return $WidgetBackgroundActive;
    }
    @if $c == background-checked {
        @return $WidgetBackgroundChecked;
    }
    @if $c == foreground-hover {
        @return $WidgetForegroundHover;
    }
    @if $c == foreground-active {
        @return $WidgetForegroundActive;
    }
    @if $c == foreground-checked {
        @return $WidgetForegroundChecked;
    }
    @if $c == border {
        @return $WidgetBorderNormal;
    }
    @if $c == border-hover {
        @return $WidgetBorderHover;
    }
    @if $c == border-focus {
        @return $WidgetBorderFocus;
    }
}

// Bare(No border) Widget Color
@function internal_bare($c: background) {
    @if $c == background {
        @return $BareBackgroundNormal;
    }
    @if $c == foreground {
        @return $BareForegroundNormal;
    }
    @if $c == background-hover {
        @return $BareBackgroundHover;
    }
    @if $c == background-active {
        @return $BareBackgroundActive;
    }
    @if $c == background-checked {
        @return $BareBackgroundChecked;
    }
    @if $c == foreground-hover {
        @return $BareForegroundHover;
    }
    @if $c == foreground-active {
        @return $BareForegroundActive;
    }
    @if $c == foreground-checked {
        @return $BareForegroundChecked;
    }
}

// Selection Color
@function internal_selection($c: background) {
    @if $c == background {
        @return $SelectionBackgroundNormal;
    }
    @if $c == foreground {
        @return $SelectionForegroundNormal;
    }
    @if $c == hover {
        @return $SelectionDecorationHover;
    }
    @if $c == backdrop {
        @return transparentize($SelectionBackgroundNormal, 0.5);
    }
}

// View Color
@function internal_view($c: background) {
    @if $c == background {
        @return $ViewBackgroundNormal;
    }
    @if $c == foreground {
        @return $ViewForegroundNormal;
    }
    @if $c == border {
        @return $ViewBorderNormal;
    }
    @if $c == border-focus {
        @return $ViewBorderFocus;
    }
    
}

// Window Color
@function internal_window($c: background) {
    @if $c == background {
        @return $WindowBackgroundNormal;
    }
    @if $c == foreground {
        @return $WindowForegroundNormal;
    }
    @if $c == border {
        @return $WindowBorderNormal;
    }
}

// Titlebar color
@function internal_titlebar($c: foreground) {
    @if $c == foreground {
        @return $WMForegroundNormal;
    }
    @if $c == background {
        @return $WMBackgroundNormal;
    }
    @if $c == border {
        @return $WMBorderNormal;
    }
}

@function internal_tooltip($c: foreground) {
    @if $c == foreground {
        @return $TooltipForegroundNormal;
    }
    @if $c == background {
        @return $TooltipBackgroundNormal;
    }
    @if $c == border {
        @return mix($TooltipForegroundNormal, $TooltipBackgroundNormal, 25%);
    }
}

@function internal_button_gradient($c, $state: background) {
    @if $state == background {
        @return linear-gradient(
        to bottom,
        change-color($c, $lightness: min(lightness($c) * 1.01, 100%)),
        change-color($c, $lightness: min(lightness($c) / 1.03, 100%))
        );
    } @else {
        @return linear-gradient(
        to bottom,
        change-color($c, $lightness: min(lightness($c) * 1.03, 100%)),
        change-color($c, $lightness: min(lightness($c) / 1.1, 100%))
        );
    }
}

$border_color: mix($WindowBackgroundNormal, $WindowForegroundNormal, 75%);
$menu_color: internal_window(background);

$arrow_down: 'go-down-symbolic';
$arrow_left: 'go-previous-symbolic';
$arrow_right: 'go-next-symbolic';
$arrow_up: 'go-up-symbolic';

$trough_color: gtkmix(gtk("@theme_bg_color"), gtk("@theme_fg_color"), 0.7);
$trough_color_backdrop: gtkmix(
    gtk("@theme_unfocused_bg_color"),
    gtk("@theme_unfocused_fg_color"),
    0.7
);
$trough_color_insensitive: gtkmix(
    gtk("@insensitive_bg_color"),
    gtk("@insensitive_fg_color"),
    0.7
);
$trough_color_backdrop_insensitive: gtkmix(
    gtk("@insensitive_unfocused_bg_color"),
    gtk("@insensitive_unfocused_fg_color"),
    0.7
);

$scrollbar_color: gtkmix($trough_color, gtk("@theme_text_color"), 0.5);
$scrollbar_color_backdrop: gtkmix(
    $trough_color_backdrop,
    gtk("@theme_unfocused_text_color"),
    0.5
);

//
// Color effects
//
//
// Color maps for color effect functions
//
$Color: (
    Disabled: $DisabledColor,
    Inactive: $InactiveColor,
);
$ColorAmount: (
    Disabled: $DisabledColorAmount,
    Inactive: $InactiveColorAmount,
);
$ColorEffect: (
    Disabled: $DisabledColorEffect,
    Inactive: $InactiveColorEffect,
);
$ContrastAmount: (
    Disabled: $DisabledContrastAmount,
    Inactive: $InactiveContrastAmount,
);
$ContrastEffect: (
    Disabled: $DisabledContrastEffect,
    Inactive: $InactiveContrastEffect,
);
$IntensityAmount: (
    Disabled: $DisabledIntensityAmount,
    Inactive: $InactiveIntensityAmount,
);
$IntensityEffect: (
    Disabled: $DisabledIntensityEffect,
    Inactive: $InactiveIntensityEffect,
);

$r: 3px; // standard radius

@function internal_insensitive($c) {
    @return internal_Color(
        internal_Intensity(internal_Contrast($c, Disabled), Disabled),
        Disabled
    );
}

@function internal_backdrop($c) {
    @if $InactiveEnable == True {
        @return internal_Color(
            internal_Intensity(internal_Contrast($c, Inactive), Inactive),
            Inactive
        );
    } @else {
        @return $c;
    }
}

@function internal_Contrast($c, $state) {
    @if map-get($ContrastEffect, $state) == 0 {
        @return $c;
    }

    @if map-get($ContrastEffect, $state) == 1 or map-get($ContrastEffect, $state) ==2 {
        @if lightness(internal_window()) > lightness(internal_window(foreground)) {
            @if lightness($c) < lightness(internal_window()) or
                ($c == internal_titlebar(foreground) and lightness(internal_titlebar(background)) <= lightness(internal_titlebar(foreground)))
            {
                @return transparentize($c, map-get($ContrastAmount, $state));
            } @else {
                @return $c;
            }
        }
        @if lightness(internal_window()) <= lightness(internal_window(foreground)) {
            @if lightness($c) > lightness(internal_window()) or
                ($c == internal_titlebar(foreground) and lightness(internal_titlebar(background)) > lightness(internal_titlebar(foreground)))
            {
                @return transparentize($c, map-get($ContrastAmount, $state));
            } @else {
                @return $c;
            }
        }
    }
}

@function internal_Color($c, $state) {
    @if map-get($ColorEffect, $state) ==0 {
        @return $c;
    }

    @if map-get($ColorEffect, $state) ==1 {
        @if map-get($ColorAmount, $state) >=0 {
            @return desaturate($c, percentage(map-get($ColorAmount, $state)));
        } @else {
            @return saturate($c, percentage(abs(map-get($ColorAmount, $state))));
        }
    }

    @if map-get($ColorEffect, $state) ==2 or map-get($ColorEffect, $state) ==3 {
        // 2 and 3 seem to do the same
        @return mix(
            map-get($Color, $state),
            $c,
            percentage(map-get($ColorAmount, $state))
        );
    }
}

@function internal_Intensity($c, $state) {
    @if map-get($IntensityEffect, $state) == 0 {
        @return $c;
    }
    @if map-get($IntensityEffect, $state) == 1 {
        @if map-get($IntensityAmount, $state) >= 0 {
            @return mix(white, $c, percentage(map-get($IntensityAmount, $state)));
        } @else {
            @return mix(
                black,
                $c,
                percentage(abs(map-get($IntensityAmount, $state)))
            );
        }
    }

    @if map-get($IntensityEffect, $state) == 2 {
        @if map-get($IntensityAmount, $state) == -1 {
            @return change-color($c, $lightness: 100%);
        } @else {
            @return change-color(
                $c,
                $lightness:
                min(lightness($c) / (1 + map-get($IntensityAmount, $state)), 100%)
            );
        }
    }

    @if map-get($IntensityEffect, $state) == 3 {
        @return change-color(
            $c,
            $lightness:
                min(lightness($c) * (1 + map-get($IntensityAmount, $state)), 100%)
        );
    }
}

@function internal_alpha2solid($c, $background) {
    $amount: alpha($c);
    $c: rgb(red($c), green($c), blue($c));
    @return mix($c, $background, $amount);
}